package com.fjnu.web;

import com.fjnu.entity.Tag;
import com.fjnu.service.TagService;
import com.fjnu.vo.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blog-api/v0.1")
public class TagController {

    @Autowired
    private TagService tagService;

    @GetMapping("/tag/list")
    public ResponseResult list() {
        try {
            log.info("查询标签列表");
            List<Tag> tags = tagService.getAllTags();
            return ResponseResult.success(tags);
        } catch (Exception e) {
            log.error("查询标签列表失败: {}", e.getMessage());
            return ResponseResult.error("查询标签列表失败");
        }
    }

    @PostMapping("/tag/add")
    public ResponseResult add(@RequestBody Tag tag) {
        try {
            log.info("新增标签 - 标签名称: {}", tag.getName());

            int result = tagService.addTag(tag);
            if (result > 0) {
                return ResponseResult.success("标签添加成功");
            } else {
                return ResponseResult.error("标签添加失败");
            }
        } catch (Exception e) {
            log.error("新增标签失败: {}", e.getMessage());
            return ResponseResult.error("新增标签失败");
        }
    }

    @GetMapping("/tag/delete")
    public ResponseResult delete(@RequestParam String tagId) {
        try {
            log.info("删除标签 - 标签ID: {}", tagId);

            int result = tagService.deleteTag(Integer.parseInt(tagId));
            if (result > 0) {
                return ResponseResult.success("标签删除成功");
            } else {
                return ResponseResult.error("标签删除失败");
            }
        } catch (Exception e) {
            log.error("删除标签失败: {}", e.getMessage());
            return ResponseResult.error("删除标签失败");
        }
    }
}